package config

import (
	"context"
	"fmt"
	"os"
	"shop-manager/utils"

	"gopkg.in/yaml.v3"
)

var (
	globalConfig = &Config{}
)

func Init(ctx context.Context, configFile string) (err error) {
	data, err := os.ReadFile(configFile)
	if err != nil {
		return fmt.Errorf("read config file failed, err: %v", err)
	}
	if err = yaml.Unmarshal([]byte(data), &globalConfig); err != nil {
		return fmt.Errorf("unmarshal config file failed, err: %v", err)
	}
	fmt.Printf("config : %+v\n", utils.JSONString(globalConfig))
	return nil
}

func Get() *Config {
	return globalConfig
}

type Config struct {
	DB     *DBConfig     `yaml:"db"`
	Server *ServerConfig `yaml:"server"`
}
type DBConfig struct {
	DSN         string `yaml:"dsn"`
	AutoMigrate bool   `yaml:"auto_migrate"`
}

type ServerConfig struct {
	Addr string `yaml:"addr"`
}
